package note;
//Description:一些方法（递归，二分查找，java的Array类
import java.util.Arrays;

public class Note4_2 {
    public static void main1(String[] args) {
        System.out.println(resove(15));
    }
    public static int  resove(int n){
        if(n == 0) {//终止条件
            return 0;
        }
        return  n % 10 +resove(n / 10);//推导递推公式（n%10+(n/10)%10）
    }

    public static void main(String[] args) {
        int[] array = {1,5,4,45,2,7};
        int[] array2 = new int[5];//new：关键字  实例化一个对象
        int[] array3 = new int[]{1,2,3,4,5};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
        /**
         * 增强for循环  for循环和for each循环区别 for each拿不到下标
         * for each更多用到集合中
         */

        for (int x:array) {
            System.out.print(x + " ");
        }
        //java的操作工具类 Arrays   toString:将参数的数组以字符串的形式输出
        String ret = Arrays.toString(array);
        System.out.println(ret);
        int[] array4 = null;//这个引用指向空  引用存储的是变量的地址 一个引用只能保存一个对象的地址
        //System.out.println(array4.length);空指针异常
        int[] a = {1,2,3};
        int[] b = a;//b 这个引用指向了a这个引用指向的对象
        int[] ret2 = fn(array);//必须使用新的引用变量接受int[]返回的值(数组作为返回值)
        System.out.println(Arrays.toString(ret2));
        myToString1(array);
        myToString(array);
        System.out.println(maxNum(array));
        System.out.println(binarySearch(2,a));
        System.out.println(Arrays.binarySearch(a, 2));
    }
    public static void func1(int[] array){
        array = new int[]{12,5,4,8,7};//改变形参的对象 而没有改变实参的对象
        //array[0] = 444;未创建对象改其引用会改变实参对象
    }
    public static int[] fn(int[] array){
        int[] ret = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ret[i] = 2 * array[i];
        }
        return  ret;
    }
    //自己写的
    public static  String myToString1(int[] array){
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            if(i == array.length - 1){
                System.out.print(array[i]);
                break;
            }
            System.out.print(array[i] + ",");
        }
        System.out.print("]");
        return "";
    }
    //老师写的
    public static String myToString(int[] array){
        if(array == null)return "null";
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str = str + array[i];
            if(i != array.length - 1){
                str = str + ",";
            }
        }
        str = str + ",";
        return str;
    }
    public  static int maxNum(int[] array){
        if(array == null) return -1;//业务处理
        if(array.length == 0) return -1;
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max <array[i]){
                max = array[i];
            }
        }
        return max;
    }
    //二分查找
    public static int binarySearch(int key,int[] array){
        int left = 0;
        int right = array.length - 1;
        while(left <= right){
            int mid = left + right >> 1;
            if(array[mid] > key){
                right = mid - 1;
            }else if(array[mid] < key){
                left = mid + 1;
            }else{
                return mid;
            }

        }
        return -1;
    }


}
